require("behaviours/wander")
require("behaviours/doaction")
require("behaviours/chaseandram")
require("behaviours/chaseandattack")
require("behaviours/standandattack")
require("behaviours/leash")

local MAX_CHASE_TIME = 10
local GIVE_UP_DIST = 20
local MAX_CHARGE_DIST = 60

local wandertimes =
{
    minwalktime = 5,
    randwalktime =  3,
    minwaittime = 0,
    randwaittime = 0,
}

local function GetWanderPos(inst)
    local target = inst:GetNearestPlayer(false) or inst
    return target:GetPosition()
end

local function ShouldramAttack(inst)
    if inst.components.combat.target ~= nil then
        return BufferedAction(inst, inst.components.combat.target, ACTIONS.ATTACK)
    end
end

local MamiBrain = Class(Brain, function(self, inst)
    Brain._ctor(self, inst)
end)

function MamiBrain:OnStart()
    local root =
        PriorityNode(
        {
            WhileNode(function() return
                not self.inst.components.combat:InCooldown()
            end, "Attack",
            DoAction(self.inst, ShouldramAttack, "Attack", false)),
            Wander(self.inst, GetWanderPos, 20, wandertimes),
        }, .25)

    self.bt = BT(self.inst, root)
end

return MamiBrain
